//存放交互逻辑
//Javascript模块化
//seckill.detail.init(params)
var seckill = {
		//封装秒杀相关ajax的url
		URL : {
			now: function(){
				return '/seckill/time/now';
			},
			exposer: function(seckillId){
				return '/seckill/'+seckillId+'/exposer';
			},
			execution: function(seckillId, md5){
				return '/seckill/'+seckillId+'/'+md5+'/execution';
			}
		},
		handlerSeckill : function(seckillId, node){
			//获取秒杀地址，控制秒杀逻辑，执行秒杀
			node.hide().html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
			$.post(seckill.URL.exposer(seckillId), {}, function(result){
				//回调函数种执行交互流程
				if(result && result['success']){
					var exposer = result['data'];
					if(exposer['exposed']){
						var md5 = exposer['md5'];
						var killUrl = seckill.URL.execution(seckillId, md5);
						$('#killBtn').one('click', function(){
							//1: 禁用按钮
							$(this).addClass('disabled');
							//2：发送秒杀请求
							$.post(killUrl, {}, function(result){
								if(result && result['success']){
									var killResult = result['data'];
									var state = killResult['state'];
									var stateInfo = killResult['stateinfo'];
									//3：显示秒杀结果
									node.html('<span class="label label-success">'+ stateInfo + '</span>');
								}
							});
						});
						node.show();
					}else{
						//未开启秒杀
						var now = exposer['now'];
						var start = exposer['start'];
						var end = exposer['end'];
						//重新进入计时
						seckill.countdown(seckillId, now, start, end);
					}
				}else{
					console.log('result='+result);
				}
			});
		},
		//校验手机
		validatePhone: function(phone){
			if(phone && phone.length == 11 && !isNaN(phone)){
				return true;
			}else{
				return false;
			}
		},
		countdown: function(seckillId, nowTime, startTime, endTime){
			var seckillBox = $('#seckill-box');
			if(nowTime > endTime){
				//秒杀结束
				seckillBox.html('秒杀结束了');
			}else if(nowTime < startTime){ 
				//秒杀未开始,计时时间绑定
				var killTime = new Date(startTime + 1000);//时间偏移，防止
				seckillBox.countdown(killTime, function(event){
					var format = event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
					seckillBox.html(format);
				}).on('finish.countdown', function(){
					//获取秒杀地址，控制秒杀逻辑，执行秒杀
					seckill.handlerSeckill(seckillId, seckillBox);
				});
			}else{
				//秒杀开始
				seckill.handlerSeckill(seckillId, seckillBox);
			}
		},
		//详情页秒杀逻辑
		detail: {
			//详情页初始化
			init : function(params){
				//后记验证和登录，计时交互，规划我们的交互流程
				//在cookie种查找手机号
				var killPhone = $.cookie('killPhone');
				
				//验证手机
				if(!seckill.validatePhone(killPhone)){
					//绑定phone
					var killPhoneModal = $('#killPhoneModal');
					killPhoneModal.modal({
						show: true, //显示弹出层
						backdrop: 'static', //禁止位置关闭
						keyboard: false //关闭键盘事件
					});
					$('#killPhoneBtn').click(function(){
						var inputPhone = $('#killPhoneKey').val();
						console.log("Inputphone: " + inputPhone);
						if(seckill.validatePhone(inputPhone)){
							$.cookie('killPhone', inputPhone, {expires: 7, path: '/seckill'});
							window.location.reload();
						}else{
							$('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误！</label>').show(300);
						}
						
					});
				}
				var startTime = params['startTime'];
				var endTime = params['endTime'];
				var seckillId = params['seckillId'];
				
				//成功登录
				$.get(seckill.URL.now(), {}, function(result){
					console.log("success in login " + result);
					if(result && result['success']){
						var nowTime = result['data'];
						console.log('nowTime=' + nowTime);
						//时间判断
						seckill.countdown(seckillId, nowTime, startTime, endTime);
					}else{
						console.log('result:' + result);
					}
				});
			}
		}
}